package com.hj.saapp.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hj.saapp.R;

/**
 * Created by 津 on 2016/4/30.
 */
public class CleanEditText extends LinearLayout {


    private ImageView leftImg;
    private EditText et_text;
    private ImageView rightImg;
    private LinearLayout root;
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //在输入框没有输入时不显示清除图标，有输入后显示
            if (TextUtils.isEmpty(s)) {
                rightImg.setVisibility(View.INVISIBLE);
            } else {
                rightImg.setVisibility(View.VISIBLE);
            }
        }
    };

    public CleanEditText(Context context) {
        super(context);
    }

    public CleanEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.weight_clean_edittext, null);
        leftImg = (ImageView) view.findViewById(R.id.iv_left);
        rightImg = (ImageView) view.findViewById(R.id.iv_right);
        et_text = (EditText) view.findViewById(R.id.et_text);
        root = (LinearLayout) view.findViewById(R.id.root);
        et_text.setLongClickable(false);
        et_text.setTextIsSelectable(false);


        et_text.addTextChangedListener(textWatcher);
        rightImg.setVisibility(View.INVISIBLE);

        rightImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_text.setText("");
            }
        });

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CleanEditText);

        //leftImg
        int leftImageR = ta.getResourceId(R.styleable.CleanEditText_leftImgR, -1);
        if (leftImageR == -1) {
            leftImg.setVisibility(View.GONE);
        } else {
            leftImg.setImageResource(leftImageR);
        }

        int rootBg = ta.getResourceId(R.styleable.CleanEditText_bg, -1);
        if (rootBg != -1) {
            root.setBackgroundResource(rootBg);
        }

        //rightImg
        int rightImageR = ta.getResourceId(R.styleable.CleanEditText_rightImgR, -1);
        if (rightImageR == -1) {
            rightImg.setVisibility(View.GONE);
        } else {
            rightImg.setImageResource(rightImageR);
        }

        //hint
        int resourceHint = ta.getResourceId(R.styleable.CleanEditText_hint, 0);
        et_text.setHint(resourceHint > 0 ? getResources().getText(resourceHint) : ta.getString(R.styleable.CleanEditText_hint));

        //text
        int resourceText = ta.getResourceId(R.styleable.CleanEditText_text, 0);
        et_text.setText(resourceText > 0 ? getResources().getText(resourceText) : ta.getString(R.styleable.CleanEditText_text));

        //inputType
        int resourceInputType = ta.getInt(R.styleable.CleanEditText_inputType, -1);
        if (resourceInputType == 1) {//phone
            et_text.setInputType(InputType.TYPE_CLASS_PHONE);
        } else if (resourceInputType == 2) {//password
            et_text.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD | InputType.TYPE_CLASS_NUMBER);
        } else if (resourceInputType == 3) {//text
            et_text.setInputType(InputType.TYPE_CLASS_TEXT);
        } else if (resourceInputType == 4) {//number
            et_text.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        // maxLength
        int maxLength = ta.getInt(R.styleable.CleanEditText_maxLength, -1);
        if (maxLength != -1) {
            et_text.setFilters(new InputFilter.LengthFilter[]{new InputFilter.LengthFilter(maxLength)});
        }

        // textSize
        float textSize = ta.getDimension(R.styleable.CleanEditText_textSize, -1);
        if (textSize != -1) {
            et_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }


        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.addView(view,layoutParams);

        ta.recycle();
    }

    public Editable getText() {
        return et_text.getText();
    }

    public void setText(int resid) {
        et_text.setText(resid);
    }

    public EditText getEditText() {
        return et_text;
    }

    public void setText(CharSequence text) {
        et_text.setText(text);
    }

    public void setImeOptions(int imeOptions) {
        et_text.setImeOptions(imeOptions);
    }

    public void addTextChangedListener(TextWatcher textWatcher) {
        et_text.addTextChangedListener(textWatcher);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        et_text.setEnabled(enabled);
    }

}
